package com.ld.manageBook.service.impl;

import com.ld.manageBook.DAO.UserDAO;
import com.ld.manageBook.bean.User;
import com.ld.manageBook.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService{
	private UserDAO userDAO;
	{
		userDAO = new UserDAO();
	}
	public User getUserByNameAndPsw(String userName, String password) {
		User user = userDAO.querySingle("select * from user where username = ? and password = ?", User.class, userName, password);
		return user;
	}
	public List<User> getAllUsers(){
		List<User> users = userDAO.queryMulti("select * from user", User.class);
		return users;
	}

	@Override
	public int deleteUserById(Integer id) {
		return userDAO.update("delete from user where id = ?",id);
	}
	public User getUserById(Integer id){
		return userDAO.querySingle("select * from user where id = ?",User.class,id);
	}
	public int changeUserById(User user){
		return userDAO.update("update user set username = ?,password = ?,status = ? where id = ?",user.getUsername(),user.getPassword(),user.getStatus(),user.getId());
	}

	@Override
	public int addUser(User user) {
		return userDAO.update("insert into user(username,password,status) values(?,?,?)",user.getUsername(),user.getPassword(),user.getStatus());
	}

	@Override
	public User getUserByName(String name) {
		return userDAO.querySingle("select * from user where username = ?",User.class,name);
	}
}
